package com.woniu.model.gh.utils;

import com.woniu.model.gh.common.enums.RedisConstant;
import com.woniu.model.gh.common.enums.impl.BusinessCode;
import com.woniu.model.gh.common.exception.Asserts;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//从缓存中获取指定数据字典的值
public class DictionaryUtil {

    public static List<TypeVo> getDictionaryList(RedisTemplate redisTemplate,Integer typeId){
        List<Map<String,Object>> reList = redisTemplate.opsForList().range(RedisConstant.DICTIONARY_LIST, 0, -1);
        List<TypeVo> list = new ArrayList<>();
        Asserts.error(reList == null, BusinessCode.DICTIONARY_LIST_NULL);
        reList.forEach(n->{
            TypeVo vo = new TypeVo();
            n.forEach((key, value) -> {
                if (key.equals("id")) vo.setId(Long.valueOf(value.toString()));
                if (key.equals("dictionaryTypeId")) vo.setTypeId(Long.valueOf(value.toString()));
                if (key.equals("content")) vo.setContent(value.toString());
                if (key.equals("value")) vo.setValue(Long.valueOf(value.toString()));
            });
            list.add(vo);
        });

        return list.stream().filter(n -> n.getTypeId()==typeId.longValue()).collect(Collectors.toList());

    }

}
